package com.speedricemanager.activity.home.registeframent.presenter;

import android.content.Context;
import android.content.Intent;
import android.view.Gravity;
import android.widget.Toast;

import com.jbm.core.iface.IAcross;
import com.jbm.jsm.iface.TaskMouldEntity;
import com.qmuiteam.qmui.widget.dialog.QMUITipDialog;
import com.speedricemanager.activity.home.fragmenadapter.RegisterauditAdapter;
import com.speedricemanager.activity.home.intface.IntegerFaceOr;
import com.speedricemanager.activity.home.registeframent.mode.RegisterauditFramentMode;
import com.speedricemanager.activity.home.registeframent.view.RegisterauditFrament_Inter;
import com.speedricemanager.activity.home.registermodepart.RegisterModePart;
import com.speedricemanager.interfaces.DialogSuccess;
import com.speedricemanager.other.base.BaseAty;
import com.vondear.rxtools.view.RxToast;
import com.wuxiaolong.pullloadmorerecyclerview.PullLoadMoreRecyclerView;

import java.util.ArrayList;

/**
 * Created by 李征 on 2017/12/17.
 */

public class RegisterauditFramentPersenter implements RegisterauditFramentPersenter_Inter{
    private Context context;
    private RegisterauditFrament_Inter registerauditFrament_inter;
    private int star = 0;
    private int pag = 10;
    ArrayList<TaskMouldEntity> arrayList;
    RegisterauditAdapter registerauditAdapter;
    private RegisterauditFramentMode registerauditFramentMode;
    public RegisterauditFramentPersenter(Context context, RegisterauditFrament_Inter registerauditFrament_inter) {
        this.context = context;
        this.registerauditFrament_inter = registerauditFrament_inter;
        registerauditFramentMode=new RegisterauditFramentMode();

        arrayList=new ArrayList<>();
    }

    /**
     * 初始化第一次加载数据
     */
    @Override
    public void initData() {
        onLoad();
        registerauditFramentMode.getModuleList("", registerauditFrament_inter.getTags(), registerauditFrament_inter.getstatic(), "", "", star, pag, new IntegerFaceOr() {
            @Override
            public void getSucces(Object o) {
                final TaskMouldEntity ta=(TaskMouldEntity)o;
               loadDismiss();
                if (ta.getMouldList()!=null){
                    registerauditFrament_inter.dianfupullLoadMoreRecyclerView().setPullLoadMoreCompleted();
                    if (star==0){
                        arrayList.clear();
                        arrayList.addAll(ta.getMouldList());
                        registerauditAdapter=new RegisterauditAdapter(context,arrayList);
                        registerauditFrament_inter.dianfupullLoadMoreRecyclerView().setAdapter(registerauditAdapter);
                    }else {
                        arrayList.addAll(ta.getMouldList());
                        registerauditAdapter.getNotifinall(arrayList);
                    }

                    registerauditAdapter.setOnClickListener(new RegisterauditAdapter.onClickListener() {
                        @Override
                        public void onClickListener(TaskMouldEntity taskMouldEntity) {
                           context.startActivity(new Intent(context,RegisterModePart.class).putExtra("tmid",taskMouldEntity.getTmId()));


                        }
                    });
                    registerauditAdapter.getJujueshenqing(new RegisterauditAdapter.GetonJujue() {
                        @Override
                        public void getJuJieShenQing(final int id) {
                            ((BaseAty)context).editAlert("请填写拒绝原因", new DialogSuccess() {
                                @Override
                                public void option(String password) {
                                    ((BaseAty)context).onLoad();
                                    registerauditFramentMode.AuditTaskMoulds(id,3,password ,"","","","",null,null,"","","","",null,new IntegerFaceOr() {
                                        @Override
                                        public void getSucces(Object o) {
                                            ((BaseAty)context).loadDismiss();
                                            IAcross iAcross=(IAcross)o;

                                        }

                                        @Override
                                        public void getError(Object o) {
                                            ((BaseAty)context).loadDismiss();
                                            IAcross iAcross=(IAcross)o;
                                            ((BaseAty)context).errorAlert(iAcross.m_info);
                                        }
                                    });
//                                    registerauditFramentMode.AuditTaskMoulds();
                                }
                            });
                        }
                    });
                }else {
                    registerauditFrament_inter.dianfupullLoadMoreRecyclerView().setPullLoadMoreCompleted();
                }

            }

            @Override
            public void getError(Object o) {
                TaskMouldEntity ta=(TaskMouldEntity)o;
              loadDismiss();
                errorAlert(ta.m_info);
            }});

    }

    @Override
    public void getOnPullLoad() {
        registerauditFrament_inter.dianfupullLoadMoreRecyclerView().setOnPullLoadMoreListener(new PullLoadMoreRecyclerView.PullLoadMoreListener() {
            @Override
            public void onRefresh() {
                star=0;
                initData();
            }
            @Override
            public void onLoadMore() {
                star+=5;
                initData();
            }
        });
    }

    /**
     * 搜索模板列表
     */
    @Override
    public void seekList() {}
    //加载框
    private QMUITipDialog tipDialog;
    public void onLoad(){
        tipDialog = new QMUITipDialog.Builder(context)
                .setIconType(QMUITipDialog.Builder.ICON_TYPE_LOADING)
                .setTipWord("正在加载")
                .create();

        tipDialog.show();
    }
    public void loadDismiss(){
        if(tipDialog!=null&&tipDialog.isShowing()) {
            tipDialog.dismiss();
        }
    }
    private Toast toast;
    //错误消息提示
    public void errorAlert(String error){
            toast = RxToast.error(context, error, Toast.LENGTH_SHORT, true);
            toast.setGravity(Gravity.CENTER, 0, 0);
            toast  .show();


    }
}
